﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MySQLApplication
{
    class CommandHandler
    {
        string str_input = "";
        public void getInput()
        {
            Console.Write("-> ");                   // Just to look nice.
            str_input = Console.ReadLine();
        }

        public void processInput(List<Command> commands)
        {
            string[] str_parameters = str_input.Split(' ');
            bool bool_commandFound = false;
            if (str_input.ToLower().Equals("help") || str_input.ToLower().Equals("?") || str_input.ToLower().Equals("commands")) // No point in searchuing for commands if they need a list of them anyways.
            {
                writeCommandList(commands);
                return;                            
            }
            foreach (Command command in commands)
            {
                if (str_parameters[0].ToLower().Equals(command.str_command.ToLower())) // we convert everything to lower that way "ExPoRt" will do the same as "export" or "EXPORT".
                {
                    command.onCall(str_parameters);
                    bool_commandFound = true;
                }
            }
            if (!bool_commandFound)
            {
                Console.WriteLine("No command \'" + str_input + "\' found.  Here is a list of available commands.");
                writeCommandList(commands);
            }
        }
        public void writeCommandList(List<Command> commands)    // Self explanatory.  People need to know what commands are available to use, what they do, and what each parameter does.
            // At some point, whenever enough commands are in the application, it would be too difficult to read with the parameters or description printed as well as the name of the command.
            // Also, these are not printed alphabetically.  They should be.
        {
            foreach (Command command in commands)
            {
                Console.WriteLine("\n----------------");
                Console.WriteLine(command.str_command);
                Console.WriteLine("  " + command.str_description);
                Console.Write("Command parameters:  ");
                for (int i = 0; i < command.str_paramNames.Length; i++)
                {
                    Console.Write("<" + command.str_paramNames[i] + ">  ");
                }
                Console.WriteLine();
            }
        }
    }
}
